/**
 * @file test_shell.c
 * @author liufeng (liufeng@injoinic.com)
 * @brief test component: shell
 * @version 0.1
 * @date 2024-11-05
 *
 * @copyright Copyright (c) 2024  injoinic
 *
 */

#include "bsp_rtc.h"
#include "shell.h"
#include "stdio.h"

void time_set(uint32_t timestamp)
{
    struct tm* time = localtime(&timestamp);
    LL_RTC_TimeTypeDef gTime;
    LL_RTC_DateTypeDef gDate;

    gTime.Hours = time->tm_hour + 8;
    gTime.Minutes = time->tm_min;
    gTime.Seconds = time->tm_sec;

    gDate.Year = time->tm_year - 100;
    gDate.Month = time->tm_mon + 1;
    gDate.Day = time->tm_mday;
    gDate.WeekDay = time->tm_wday;

    LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BIN, &gTime);
    LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BIN, &gDate);
}

void time_display()
{
    LL_RTC_TimeTypeDef Time;
    LL_RTC_DateTypeDef Date;
    rtc_get(&Time, &Date);
    printf("%02d/%02d/%02d\r\n", 2000 + Date.Year, Date.Month, Date.Day);
    printf("%02d:%02d:%02d\r\n", Time.Hours, Time.Minutes, Time.Seconds);

    uint32_t timestamp = rtc_to_timestamp();
    printf("UTC timestamp:%d \r\n\r\n", timestamp);
}

SHELL_EXPORT_CMD(
    SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC),
    time_set,
    time_set,
    set RTC time by a unix timesatmp
);

SHELL_EXPORT_CMD(
    SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC),
    time_display,
    time_display,
    display RTC time
);
